package com.baseLearn.learn.arithmetic;

import io.swagger.models.auth.In;

import java.util.Arrays;
import java.util.HashMap;

public class ArrayOp {

    /**
     * 查找在数组中第一个出现2次的数值,O(n^2)
     *
     * @return
     */
    public static int findMethodA(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            for (int j = i + 1; j < arrays.length - i; j++) {
                if (arrays[i] == arrays[j]) {
                    return arrays[i];
                }
            }
        }
        return 0;
    }


    /**
     * 查找在数组中第一个出现2次的数值,O(n)
     *
     * @return
     */
    public static int findMethodB(int[] arrays) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < arrays.length; i++) {
            if (hashMap.get(arrays[i]) != null) {
                return arrays[i];
            } else {
                hashMap.put(arrays[i], arrays[i]);
            }
        }
        return 0;
    }

    /**
     * 找到一个数组中相加结果为特定数字的两个元素,比如[1，2，3，4，5], target=3。要输出的两个元素为[1,2]
     *
     * @return
     */
    public static int[] findMethodC(int[] arrays, int sum) {
        int[] newArray = new int[2];
        for (int i = 0; i < arrays.length; i++) {
            for (int j = i + 1; j < arrays.length - i; j++) {
                if (arrays[i] + arrays[j] == sum) {

                    newArray[0] = arrays[i];
                    newArray[1] = arrays[j];
                    return newArray;
                }
            }
        }
        return newArray;

    }


    public static void main(String[] args) {
        int[] arrays = {11, 3, 6, 7, 9, 1, 2, 3, 6, 7, 9};
        int[] result = findMethodC(arrays, 3);
        //  int a = findMethodA(arrays);
        System.out.println(Arrays.toString(result));
    }
}
